<?php
class Member_model extends CI_Model 
{
	
    
    var $student_id;
    var $table_name              = 'users';
	var $fields                  = array('name','gender','email_id','dob','password','landline','mobile','addressline1','addressline2','city','state','pincode','deleted');
	var $required_fields         = array('name');

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		
    }
	 function getMembers($start = 0, $limit = 10,$like,$where = array('deleted' => 0))
	 {
		 $this->db->select('users.id as sid,name,gender,email_id,mobile,landline,addressline1,addressline2,created,password,pass');
    	$this->db->order_by('users.created DESC');
    	$this->db->where($where);
		//$this->db->join('places_price','places_price.id=users.country_id','left');
		
		if(!empty($like))
		{
			$this->db->like($like);
		}
    	$query                        = $this->db->get($this->table_name,$limit,$start);
		$records                      = $query->result_array();

		$this->db->select('count(id) as total');
    	$this->db->where($where);
		if(!empty($like))
		{
			$this->db->like($like);
		}
    	$rst                           = $this->db->get($this->table_name);
		$total                         = $rst->first_row()->total;
		
		$obj->records                  = $records;
		$obj->total                    = $total;
		return $obj;
    }
	
	function required($data = array()) 
	{
    	if(empty($data)) return false;
    	$this->obj = get_instance();
    	$status                 = $this->obj->retrieve->required($this->required_fields, $data);
    	return $status;
    }
    
	function add($data = array())
	{
    	if(empty($data)) return -1;
    	if($this->required($data) ) {
			$data['password']             = md5($data['password']);
    		$status                       = $this->db->insert($this->table_name, $data);
			$this->student_id                = $this->db->insert_id();
			$status                       = array();
			$status['id']                 = $this->student_id;
			$status['status']             = 1;
			return $status;
    	} else {
    		return -2;
    	}
    }
	
	function udpate($id = '', $data = array(), $hash ='')
	{  //print_r($data);
    	if(empty($data) || empty($id)) return -1;

    	if($this->required($data)) 
		{
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			return $status;
    	} 
		else 
		{
    		return -2;
    	}
    }
	
     function login($username = '', $password = '') 
	 {
    	if(empty($username) || empty($password)) 
		{
    		return false;
    	}
    	$mypassword = md5($password);
    	$where = array('email_id' => $username, 'password' => $mypassword, 'deleted' => 0,'active'=>1);
    	$rst = $this->db->get_where($this->table_name, $where);
    	$records = $rst->row_array();
    	if(!empty($records)) 
		{
    		$CI 	= & get_instance();
    		$CI->load->helper('cookie');
    		$user['id'] = $records['id'];
    		$user['name'] = $records['name'];
    		$user['ltime'] = time();
    		$user['myhash'] = md5($records['email_id'].$CI->config->item('pwd_hash').$user['ltime']);
    		$CI->session->set_userdata('mylogin', $user);
    		$CI->session->set_userdata('mymsglogin', 'Logged In As ');
    		return true;
    		
    	}
		
    	return false;
    }
	function delete($id = '')
	{
		if(empty($id)) return -1;
		$data['deleted']               = 1;
		$this->db->where(array('id' => $id));
		$status                        = $this->db->update($this->table_name, $data);
		return $status;
    }
	function getEntries()
	{
		$where['deleted']=0;
		$this->db->where($where);
        $query = $this->db->get('forms');
        return $query->result();
				
    

	}
	function changepwd($id = '', $data = array()){
    	if(empty($data) || empty($id)) return -1;
    	if(!empty($data['password']))
    		$data['password'] = md5($data['password']);
    	else 
    		$data['password'] = '';
    	if(!empty($data['password']) || !empty($data['name'])) {
    		$this->db->where(array('id' => $id));
			$status = $this->db->update($this->table_name, $data);
			return $status;
    	} else {
    		return -2;
    	}
    }
   
    function activate($id)
	{
		$data = array(
               'active' => 1
            );
		$this->db->where('id', $id);
		$status = $this->db->update($this->table_name, $data); 
		return $status;

	}
  

}

?>